package com.abigdreamer.im.client.module.talk.file;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

import com.abigdreamer.im.client.module.messageProcess.IMessageProcess;
import com.abigdreamer.im.client.module.talk.TalkWindow;
import com.abigdreamer.im.client.module.vo.Cache;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.im.message.BaseMessage;
import com.abigdreamer.im.message.FileRequestMessage;
import com.abigdreamer.net.p2p.FileProcessListener;
import com.abigdreamer.net.p2p.FileReceiver;

public class FileRequestProcess implements IMessageProcess, Runnable {

	private FileRequestMessage requestMessage;

	public void processMessage(BaseMessage message) {
		FileRequestMessage requestMessage = (FileRequestMessage) message;
		FileRequestProcess requestMessageProcess = new FileRequestProcess();
		requestMessageProcess.setRequestMessage(requestMessage);
		try {
			SwingUtilities.invokeAndWait(requestMessageProcess);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	public void run() {

		String friendQQ = requestMessage.getSrcQQ();
//		String owerQQ = requestMessage.getDestQQ();
		Cache cache = Cache.getInstance();
		final TalkWindow qqTalkFrame = cache.getTalkFrame(friendQQ);
		if (!qqTalkFrame.isVisible()) {
			qqTalkFrame.setVisible(true);
		}
		qqTalkFrame.addFileJPanel(true, requestMessage);

		String msg = "你是否接受" + requestMessage.getFileName() + " 大小为:" + requestMessage.getFileLength() / (1024 * 1024.0) + "MB";
		int result = javax.swing.JOptionPane.showConfirmDialog(qqTalkFrame, msg);

//		FileResponseMessage responseMessage = new FileResponseMessage(owerQQ, friendQQ);
//		responseMessage.setFileLength(requestMessage.getFileLength());
//		responseMessage.setFileAbsolutePath(requestMessage.getFileAbsolutePath());
//		responseMessage.setFileName(requestMessage.getFileName());

		String saveFilePatn = "";
		if (result == 0) {

			EIMLogger.INFO("此用户接受文件传输请求");
			//responseMessage.setFileResponse(MessageType.FileResponse_Accepted);
			javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
			fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
			fileChooser.showOpenDialog(qqTalkFrame);
		
			File selectedFile = fileChooser.getSelectedFile();
			
			if(selectedFile.isDirectory()) {
				saveFilePatn = selectedFile.getAbsolutePath() + "\\" + requestMessage.getFileName();
			} else {
				saveFilePatn = fileChooser.getCurrentDirectory() + "\\" + requestMessage.getFileName();
			}
		
			//responseMessage.setSaveFilePath(saveFilePatn);
			EIMLogger.INFO("此用户保存的路劲为：" + saveFilePatn);
			EIMLogger.INFO("服务器ip：" + requestMessage.getIp() + ",port:" + requestMessage.getPort());
			
			Long fileLen = requestMessage.getFileLength();
			
			FileReceiver receiver = new FileReceiver(requestMessage.getIp(), requestMessage.getPort(), saveFilePatn, fileLen.intValue(), new FileProcessListener() {
				
				@Override
				public void onProcess(int currentPackageIndex, int totalPackageCount) {
					qqTalkFrame.getFileNode().setProcess(currentPackageIndex, totalPackageCount);
					qqTalkFrame.getFileNode().hideActionBox();
				}
				
				@Override
				public void onExit() {
					qqTalkFrame.removeFileNode();
				}
			});
			new Thread(receiver).start();
			
			System.out.println("trans file:" + saveFilePatn + " success...");

		} else {
			EIMLogger.INFO("此用户拒绝文件传输请求");
//			responseMessage.setSaveFilePath(saveFilePatn);
//			responseMessage.setFileResponse(MessageType.FileResponse_Decline);
		}
	}

	public FileRequestMessage getRequestMessage() {
		return requestMessage;
	}

	public void setRequestMessage(FileRequestMessage requestMessage) {
		this.requestMessage = requestMessage;
	}

}
